var member
var currentCount
$(function () {
    init()
    changBankcode()
    changPhonecode()
    erweima()
    orderInfo(1)
})
function init() {
    //获取用户信息
    $.post('/member/refresh', function (data) {
        member = data.obj
        console.log(member);
        var phoneXing = member.phone.substring(0, 3) + "******" + member.phone.substring(9, 11);
        if (member.idCard !== null && member.idCard !== '') {
            $('#bkOpen .layui-input').eq(1).val(member.idCard.substring(0, 5) + "**********" + member.idCard.substring(14))
        }
        $('.formPhone').html(phoneXing)//修改密码显示的手机
        $('.individual-text-name strong').html(phoneXing)
        $('.safe-name strong').html(phoneXing)
        $('.safa-phone h4 span').html('您绑定的手机号是：' + phoneXing)
        $('#changeMobile').val(member.phone)//修改密码隐藏域的手机
        $('#bkOpen .layui-input').eq(0).val(member.realName)
        $('#bkOpen .layui-input').eq(2).val(phoneXing)

//欢迎
        $('.userName').html(phoneXing)
//是否实名
        if (member.realName !== null && member.realName !== '') {
            $('.realChange').css('display', 'none')
            $('.c-realName').html('已认证')
            $('.i-card span').html('您的实名信息已认证')
        }
//是否绑定了银行卡
        if (member.bankCard !== null && member.bankCard !== '') {
            $('.c-bankc').html('')
            $('.i-bank span').html('您已添加银行卡')
        }
    })
}
//获取二维码
function erweima() {
    $.post('/member/qrcode', function (data) {
        if (data.code == 10007) {
            $('#erweima img').prop('src', 'data:image/png;base64,' + data.obj)
            $('.infoBottom p').html('请扫描二维码申请贷款')
        } else if (data.code == 20014 || data.code == 20015) {
            $('.infoBottom p').html('请先完成实名认证并绑定银行卡信息后刷新获取二维码')
            $('#erweima').css('display', 'none')
        } else {
            $('.infoBottom p').html('系统异常，请稍后刷新获取')
            $('#erweima').css('display', 'none')
        }

    })
}
//获取订单信息
function orderInfo(currentPage) {
    $.post('/orderInfo/list',{'currentPage':currentPage,'size':10}, function (data) {
        if (data.code == 10011) {
            if (data.obj.count > 0) {
                currentCount=data.obj.count
                $('.orderEmpty').css('display', 'none')
                $('#orderBottom').css('display', 'block')
                $('#orderTable tbody').empty();
                for (var i = 0; i < data.obj.orderInfos.length; i++) {
                    $('#orderTable tbody').append('<tr><td>' + data.obj.orderInfos[i].orderTime + '</td> <td>' + data.obj.orderInfos[i].money + '</td><td>' + data.obj.orderInfos[i].education + '</td> </tr>')
                }
            }
        } else {
            layer.msg("用户订单信息拉取失败！系统异常！请稍候再试", {time: 3000})
        }
    })
}
//tab
var select = $('.leftBody ul').find('li')
select.on('click', function () {
    $('.minRight').hide()
    $($('.minRight')[$(this).index()]).show()
})
//个人中心hover显示有没有填写
$('.infoLeft i').on('mouseover', function () {
    $(this).find($('.layui-badge-rim')).css('display', 'block')
    $(this).on('mouseout', function () {
        $(this).find($('.layui-badge-rim')).css('display', 'none')
    })
})
//侧边栏动画
$('.leftBody li').on('mouseover', function () {
    $(this).find('i').addClass('animated swing')
    $(this).on('mouseout', function () {
        $(this).find('i').removeClass('animated swing')
    })
})
layui.use(['form','laypage'], function () {
    var form = layui.form;
    var laypage = layui.laypage;

    //订单分页
    laypage.render({
        elem: 'page',
        count: currentCount ,
        theme:'#3b9bff',
        jump: function(obj, first){
            orderInfo(obj.curr)
        //    console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
        //    console.log(obj.limit); //得到每页显示的条数
        }
    });

    //账单分页
    laypage.render({
        elem: 'billPage',
        count: currentCount ,
        theme:'#3b9bff',
        jump: function(obj, first){

        }
    });


    $('.passwordChange').on('click', function () {
        layer.open({
            type: 1,
            content: $('#pwOpen'),
            area: ['500px', '400px'],
            title: ['修改密码', 'font-size:18px;']
        });

        //改密码提交
        form.on('submit(phoneChangeSend)', function (data) {
            $.post('/retrievePwd', data.field, function (res) {
                if (res.code == 10003) {
                    layer.closeAll()
                    layer.msg("密码修改成功", {time: 3000})
                } else if (res.code == 10002) {
                    layer.msg("您的手机号尚未注册!", {time: 3000})
                } else if (res.code == 20006) {
                    layer.msg("请输入正确的短信验证码!", {time: 3000})
                }
            })
            return false;
        });
    })

    $('.backcardChange').on('click', function () {//修改银行卡
        if (member.realName !== '' && member.realName !== null) {
            layer.open({
                type: 1,
                content: $('#bkOpen'),
                area: ['754px', '711px'],
                title: ['添加银行卡', 'font-size:18px;']
            });

            //银行卡提交
            form.on('submit(bkChangeSend)', function (data) {
                $.post('/member/bindBank', data.field, function (res) {
                    if (res.code == 10009) {
                        layer.closeAll()
                        init()
                        layer.msg("恭喜您银行卡绑定成功", {time: 3000})
                    } else {
                        layer.msg(res.message, {time: 3000})
                    }
                })
                return false;
            });
        } else {
            layer.msg('请先实名认证', {time: 3000})
        }

    })
    $('.realChange').on('click', function () {//修改实名
        layer.open({
            type: 1,
            content: $('#realOpen'),
            area: ['500px', '300px'],
            title: ['实名认证', 'font-size:18px;']
        });
    })
    form.on('submit(realChangeSend)', function (data) {//提交实名
        $.post('/member/verified', data.field, function (res) {
            if (res.code == 10008) {
                layer.closeAll()
                layer.msg("实名认证成功", {time: 3000})
                init();
            } else {
                layer.msg(res.message, {time: 3000})
            }
        })
        return false;
    });
    form.verify({
        password: [
            /((?=.*\d)(?=.*\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{8,16}$/, '请输入8-16位且数字、字母、符号至少包含两种'

        ]
    })

});
//修改密码发送短信验证码
function changPhonecode() {
    $('#phoneSms').one('click', function () {
        this.innerHTML = '60秒后重新发送'
        countDown($(this))
        $.post('/sendSms', {'phone': member.phone}, function (data) {
            if (data.code != 10000) {
                layer.msg(data.message, {time: 3000})
            }
        })
    })
}

//增加银行卡发送短信验证码
function changBankcode() {
    $('#bkSms').one('click', function () {
        this.innerHTML = '60秒后重新发送'
        countDown($(this))
        $.post('/sendSms', {'phone': member.phone}, function (data) {
            if (data.code != 10000) {
                layer.msg(data.message, {time: 3000})
            }
        })
    })
}

//60秒倒计时s
function countDown(dom) {
    var timer, k = 60,
        i = 0
    timer = setInterval(function () {
        i++;
        dom.html((k - i) + '秒后重新发送')
        if ((k - i) == 0) {
            dom.html('发送验证码')
            changPhonecode()
            changBankcode()
            clearInterval(timer)
        }
    }, 1000)
}
